﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;

namespace DelonixRegiaHotel
{
    public partial class UpdateBooking : Form
    {
        string passCustomerID = "";
        string passcheckID = "";
        public UpdateBooking(string checkID)
        {
            InitializeComponent();
            timer1.Start();
            passcheckID = checkID;
            lblshowcholdername.Visible = false;
            lblcholdername.Visible = false;
            lblcnumber.Visible = false;
            cboxcnumber.Visible = false;
            btnaddccardinfo.Visible = false;
            ArrayList getBookingInfo = AdvBookingDBManager.GetBookingInfoByCheckID(checkID);
            foreach(var v in getBookingInfo)
            {
                if(v is CheckInOut)
                {
                    CheckInOut getCheckInInfo = new CheckInOut();
                    getCheckInInfo = (CheckInOut)v;
                    dtpcalender.Value = getCheckInInfo.CheckInTime;
                    tbxadult.Text = Convert.ToString(getCheckInInfo.NumOfAdult);
                    tbxchilden.Text = Convert.ToString(getCheckInInfo.NumOfChild);
                    tbxAddDays.Text = Convert.ToString(getCheckInInfo.NumOfDays);
                    lblShowCheckOut.Text = Convert.ToString(getCheckInInfo.CheckOutTime.ToLongDateString());
                    lblshowbalance.Text = Convert.ToString(getCheckInInfo.Balance);
                    tbxDeposit.Text = Convert.ToString(getCheckInInfo.Deposit);
                    tbxremarks.Text = getCheckInInfo.Remarks;
                    passCustomerID = getCheckInInfo.CustomerID;
                    if (getCheckInInfo.PaymentType.Equals("Cash"))
                    {
                        rbcash.Checked = true;
                    }
                    else
                    {
                        rbccard.Checked = true;
                    }
                }

                if(v is Customer)
                {
                    Customer getCustomerInfo = new Customer();
                    getCustomerInfo = (Customer)v;
                    lblshowfirstname.Text = getCustomerInfo.FirstName;
                    lblshowlastname.Text = getCustomerInfo.LastName;
                    lblshownric.Text = getCustomerInfo.Nric;
                    tbxshowaddress.Text = getCustomerInfo.HomeAddress;
                    lblshowpostalcode.Text = Convert.ToString(getCustomerInfo.PostalCode);
                    lblshowcountry.Text = getCustomerInfo.Country;
                    lblshowdob.Text = Convert.ToString(getCustomerInfo.Dob);
                    lblshowcontactnumber.Text = Convert.ToString(getCustomerInfo.ContactNo);
                    tbxshowemail.Text = getCustomerInfo.Email;
                    lblshowcholdername.Text = getCustomerInfo.FirstName + " " + getCustomerInfo.LastName;
                }

                if(v is Room)
                {
                    Room getRoomInfo = new Room();
                    getRoomInfo = (Room)v;
                    lblshowrmno.Text = getRoomInfo.RoomNo;
                    if (getRoomInfo.SmkRoom.Equals("Yes"))
                    {
                        rbyes.Checked = true;
                    }
                    else
                    {
                        rbno.Checked = true;
                    }
                }

                if(v is RoomType)
                {
                    RoomType getRoomTypeInfo = new RoomType();
                    getRoomTypeInfo = (RoomType)v;
                    lblshowrmtype.Text = getRoomTypeInfo.RoomTypeName;
                    lblshowrmprice.Text = Convert.ToString(getRoomTypeInfo.RoomPrice);
                    lblshowamount.Text = Convert.ToString(Convert.ToInt32(tbxAddDays.Text) * getRoomTypeInfo.RoomPrice);
                    tbxrmdetails.Text = getRoomTypeInfo.RoomDetails;
                }
            }

            if (ManageCustomerDBManager.GetCreditInfoByCustomerId(passCustomerID).Count > 0)
            {
                ArrayList listOfCreditCardInfo = new ArrayList();
                listOfCreditCardInfo = ManageCustomerDBManager.GetCreditInfoByCustomerId(passCustomerID);
                cboxcnumber.Items.Add("-Select Card-");
                foreach (CreditCardInfo cardInfo in listOfCreditCardInfo)
                {
                    cboxcnumber.Items.Add(cardInfo.CreditCardNo);
                }
                cboxcnumber.SelectedIndex = 0;
            }            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (tbxadult.Text.Equals("") || tbxchilden.Text.Equals("") || tbxAddDays.Text.Equals("") || lblShowCheckOut.Text.Equals("checkoutdate") || lblshowbalance.Text.Equals("-"))
            {
                MessageBox.Show("Please check all information are filled in");
                return;
            }

            if (rbccard.Checked == true && cboxcnumber.SelectedIndex == 0)
            {
                MessageBox.Show("Please check all information are filled in");
                return;
            }

            CheckInOut bookingInfo = new CheckInOut();
            bookingInfo.CheckID = passcheckID;
            bookingInfo.Deposit = Convert.ToDecimal(tbxDeposit.Text);
            bookingInfo.Balance = Convert.ToDecimal(lblshowbalance.Text);
            bookingInfo.CheckInTime = dtpcalender.Value.Date;
            bookingInfo.CheckOutTime = Convert.ToDateTime(lblShowCheckOut.Text);
            bookingInfo.NumOfAdult = Convert.ToInt32(tbxadult.Text);
            bookingInfo.NumOfChild = Convert.ToInt32(tbxchilden.Text);
            bookingInfo.RoomRate = Convert.ToDecimal(lblshowrmprice.Text);
            bookingInfo.Remarks = tbxremarks.Text;
            bookingInfo.RoomNo = lblshowrmno.Text;
            bookingInfo.NumOfDays = Convert.ToInt32(tbxAddDays.Text);
            if (rbcash.Checked == true)
            {
                bookingInfo.PaymentType = rbcash.Text;
            }
            else
            {
                bookingInfo.PaymentType = rbccard.Text;
            }


            if (AdvBookingDBManager.UpdateCheckInOutByCheckId(bookingInfo) == 1)
            {
                MessageBox.Show("updated checkID: " + passcheckID + " Sucessfully");
                this.Hide();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int days = int.Parse(tbxAddDays.Text);
            days = days - 1;
            string day = dtpcalender.Value.AddDays(days).ToLongDateString();
            lblShowCheckOut.Text = day;
            rbyes.Checked = false;
            rbno.Checked = false;
            lblshowrmtype.Text = "-";
            lblshowrmno.Text = "-";
            lblshowrmprice.Text = "-";
            lblshowamount.Text = "-";
            tbxDeposit.Text = "";
            lblshowbalance.Text = "-";
            tbxrmdetails.Text = "";
            tbxremarks.Text = "";

        }

        private void btncheckroom_Click(object sender, EventArgs e)
        {
            if (rbno.Checked != true && rbyes.Checked != true)
            {
                MessageBox.Show("Kindly indicate whether you want smoking or non-smoking");
            }

            else if (rbyes.Checked == true)
            {
                if (lblShowCheckOut.Text.Equals("CheckOutDate"))
                {
                    MessageBox.Show("please select check in date and add number of days staying and press add");
                    return;
                }
                //search base on smoking            
                RoomBookingAvaliability newForm = new RoomBookingAvaliability("Yes", dtpcalender.Value.Date, Convert.ToDateTime(lblShowCheckOut.Text), tbxAddDays.Text, null, this);
                newForm.ShowDialog();
            }

            else
            {
                if (lblShowCheckOut.Text.Equals("CheckOutDate"))
                {
                    MessageBox.Show("please select check in date and add number of days staying and press add");
                    return;
                }
                //search base on non smoking
                RoomBookingAvaliability newForm = new RoomBookingAvaliability("No", dtpcalender.Value.Date, Convert.ToDateTime(lblShowCheckOut.Text), tbxAddDays.Text, null, this);
                newForm.ShowDialog();
            }
        }

        private void btnDeposit_Click(object sender, EventArgs e)
        {
            if (lblshowamount.Text.Equals("-"))
            {
                MessageBox.Show("you have not selected any rooms yet, please check click on check room 1st");
                return;
            }
            double deposit = Convert.ToDouble(tbxDeposit.Text);
            double balance = Convert.ToDouble(lblshowamount.Text) - deposit;
            lblshowbalance.Text = Convert.ToString(balance);
        }

        private void rbcash_CheckedChanged(object sender, EventArgs e)
        {
            lblshowcholdername.Visible = false;
            lblcholdername.Visible = false;
            lblcnumber.Visible = false;
            cboxcnumber.Visible = false;
            btnaddccardinfo.Visible = false;
        }

        private void rbccard_CheckedChanged(object sender, EventArgs e)
        {
            if(rbccard.Checked == true)
            {
                if (ManageCustomerDBManager.GetCreditInfoByCustomerId(passCustomerID).Count == 0)
                {
                    MessageBox.Show("No Credit Card Info");
                    if ((MessageBox.Show("Do you want to create new credit card info?", "Confirm Create?", MessageBoxButtons.YesNo) == DialogResult.Yes))
                    {
                        this.Hide();
                        CreateCreditCard newMDIChild = new CreateCreditCard();
                        callForm(newMDIChild);
                        return;
                    }
                    rbcash.Checked = true;
                }
                else
                {
                    lblshowcholdername.Visible = true;
                    lblcholdername.Visible = true;
                    lblcnumber.Visible = true;
                    cboxcnumber.Visible = true;
                    btnaddccardinfo.Visible = true;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            lbltime.Text = dt.ToString();
        }

        private void dtpcalender_ValueChanged(object sender, EventArgs e)
        {
            tbxAddDays.Text = "";
            lblShowCheckOut.Text = "CheckOutDate";
            rbyes.Checked = false;
            rbno.Checked = false;
            lblshowrmtype.Text = "-";
            lblshowrmno.Text = "-";
            lblshowrmprice.Text = "-";
            lblshowamount.Text = "-";
            tbxDeposit.Text = "";
            lblshowbalance.Text = "-";
            tbxrmdetails.Text = "";
            tbxremarks.Text = "";

        }

        private void tbxCheckAllForNumbers_TextChanged(object sender, EventArgs e) // int only generalized method
        {
            Exception X = new Exception();
            TextBox T = (TextBox)sender;

            try
            {
                if (T.Text != "-")
                {
                    int x = int.Parse(T.Text);
                }
            }
            catch (Exception)
            {
                try
                {
                    int CursorIndex = T.SelectionStart - 1;
                    T.Text = T.Text.Remove(CursorIndex, 1);

                    //Align Cursor to same index
                    T.SelectionStart = CursorIndex;
                    T.SelectionLength = 0;
                }
                catch (Exception) { }
            }
        }

        private void checkWhetherIsItDoubletextbox_TextChanged(object sender, EventArgs e) // double only generalized method
        {
            Exception X = new Exception();

            TextBox T = (TextBox)sender;

            try
            {
                if (T.Text != "-")
                {
                    double x = double.Parse(T.Text);

                    if (T.Text.Contains(','))
                        throw X;
                }
            }
            catch (Exception)
            {
                try
                {
                    int CursorIndex = T.SelectionStart - 1;
                    T.Text = T.Text.Remove(CursorIndex, 1);

                    //Align Cursor to same index
                    T.SelectionStart = CursorIndex;
                    T.SelectionLength = 0;
                }
                catch (Exception) { }
            }
        }

        private void btnaddccardinfo_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Do you want to create new credit card info?", "Confirm Create?", MessageBoxButtons.YesNo) == DialogResult.Yes))
            {
                this.Hide();
                CreateCreditCard newMDIChild = new CreateCreditCard();
                callForm(newMDIChild);
            }   

        }

        Form mdiChildForm = new Form();
        public void Form1_Load(object sender, System.EventArgs e)
        {
            MdiClient ctlMDI;

            foreach (Control ctl in MainForm.ActiveForm.Controls)
            {
                try
                {
                    // Attempt to cast the control to type MdiClient.
                    ctlMDI = (MdiClient)ctl;

                    // Set the BackColor of the MdiClient control.
                    ctlMDI.BackColor = this.BackColor;
                }
                catch (InvalidCastException exc)
                {
                    // Catch and ignore the error if casting failed.
                }
            }

            // Set the IsMdiContainer property to true.
            IsMdiContainer = true;

            // Set the child form's MdiParent property to  
            // the current form.
            mdiChildForm.MdiParent = MainForm.ActiveForm;

        }

        public void callForm(Form newForm)
        {
            if (CheckOpened(newForm.Text) == false)
            {
                newForm.MdiParent = MainForm.ActiveForm;
                // Display the new form.
                newForm.StartPosition = FormStartPosition.CenterScreen;
                newForm.Show();
            }
        }

        public bool CheckOpened(string name)
        {
            FormCollection fc = Application.OpenForms;
            foreach (Form frm in fc)
            {
                if (frm.Text == name)
                {
                    return true;
                }
            }
            return false;
        }


    }
}
